package com.thd.springboottest.springbootextends.custombeandefinitionregistrypostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * com.thd.springboottest.springbootextends.custombeandefinitionregistrypostprocessor.MyBeanDefinitionRegistryPostProcessor
 *
 * @author: wanglei62
 * @DATE: 2022/4/22 14:30
 **/
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {

        System.out.println("======================= 扩展  可以在 MyBeanDefinitionRegistryPostProcessor 这里添加beanDefinition =======================");
        RootBeanDefinition beanDefinition  = new RootBeanDefinition();
        beanDefinition.setBeanClass(ExampleBeanA.class);
        beanDefinition.setScope("singleton"); // 值为singleton 或  proptotype
        MutablePropertyValues mpv = beanDefinition.getPropertyValues();
        // 注入属性 (如果有)
        mpv.addPropertyValue("name","devil13th");

        // 设置 init方法 没有就不用设置
        beanDefinition.setInitMethodName("init");
        // 设置 destory方法 没有就不用设置
        beanDefinition.setDestroyMethodName("destory");

        // 注册到ioc容器中
        beanDefinitionRegistry.registerBeanDefinition("exampleBeanA",beanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
